//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "BYRCurrency.h"
using namespace Cephei::QL::Currencies;
#include <gen/QL/Math/Rounding.h>
#include <gen/QL/Currency.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Currencies::CBYRCurrency::CBYRCurrency () : CCurrency(CBYRCurrency::typeid)
{
    try
    {
#ifdef HANDLE
        _phBYRCurrency = NULL;
#endif
        _ppBYRCurrency = new boost::shared_ptr<QuantLib::BYRCurrency> (new QuantLib::BYRCurrency ( ));
        SetCurrency (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppBYRCurrency));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Currencies::CBYRCurrency::CBYRCurrency (boost::shared_ptr<QuantLib::BYRCurrency>& childNative, Object^ owner) : CCurrency(CBYRCurrency::typeid)
{
#ifdef HANDLE
	_phBYRCurrency = NULL;
#endif
	_ppBYRCurrency = &childNative;
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppBYRCurrency));
}
Cephei::QL::Currencies::CBYRCurrency::CBYRCurrency (QuantLib::BYRCurrency& childNative, Object^ owner) : CCurrency(CBYRCurrency::typeid)
{
#ifdef HANDLE
	_phBYRCurrency = NULL;
#endif
	_ppBYRCurrency = new boost::shared_ptr<QuantLib::BYRCurrency> (&childNative);
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppBYRCurrency));
    _BYRCurrencyOwner = owner;
    _CurrencyOwner = owner;
}

Cephei::QL::Currencies::CBYRCurrency::CBYRCurrency (CBYRCurrency^ copy) : CCurrency(CBYRCurrency::typeid)
{
#ifdef HANDLE
	_phBYRCurrency = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBYRCurrency = new boost::shared_ptr<QuantLib::BYRCurrency> (copy->GetShared());
        _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppBYRCurrency));
    }
}
Cephei::QL::Currencies::CBYRCurrency::CBYRCurrency (PLATFORM::Type^ t) : CCurrency(CBYRCurrency::typeid)
{
#ifdef HANDLE
	_phBYRCurrency = NULL;
#endif
	if (!t->IsSubclassOf(CBYRCurrency::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Currencies::CBYRCurrency::CBYRCurrency (QuantLib::Handle<QuantLib::BYRCurrency>& childNative, Object^ owner)  : CCurrency(CBYRCurrency::typeid)
{
	_phBYRCurrency = &childNative;
	_ppBYRCurrency = &static_cast<boost::shared_ptr<QuantLib::BYRCurrency>>(childNative.currentLink());
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppBYRCurrency));
    _BYRCurrencyOwner = owner;
}
Cephei::QL::Currencies::CBYRCurrency::CBYRCurrency (QuantLib::Handle<QuantLib::BYRCurrency> childNative)  : CCurrency(CBYRCurrency::typeid)
{
	_phBYRCurrency = &childNative;
	_ppBYRCurrency = &static_cast<boost::shared_ptr<QuantLib::BYRCurrency>>(childNative.currentLink());
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppBYRCurrency));
}
#endif
#ifdef STRUCT
Cephei::QL::Currencies::CBYRCurrency::CBYRCurrency (QuantLib::BYRCurrency childNative)  : CCurrency(CBYRCurrency::typeid)
{
#ifdef HANDLE
	_phBYRCurrency = NULL;
#endif
	_ppBYRCurrency = new boost::shared_ptr<QuantLib::BYRCurrency> (new QuantLib::BYRCurrency (childNative));
    _ppCurrency = new boost::shared_ptr<QuantLib::Currency> (boost::dynamic_pointer_cast<QuantLib::Currency> (*_ppBYRCurrency));
}
#endif

Cephei::QL::Currencies::CBYRCurrency::~CBYRCurrency ()
{
    if (_ppBYRCurrency != NULL)
    {
	    delete _ppBYRCurrency;
        _ppBYRCurrency = NULL;
    }
}
Cephei::QL::Currencies::CBYRCurrency::!CBYRCurrency ()
{
    if (_ppBYRCurrency != NULL)
    {
	    delete _ppBYRCurrency;
    }
}
QuantLib::BYRCurrency& Cephei::QL::Currencies::CBYRCurrency::GetReference ()
{
    if (_ppBYRCurrency == NULL) throw REFNEW NativeNullException ();
	return **_ppBYRCurrency;
}
boost::shared_ptr<QuantLib::BYRCurrency>& Cephei::QL::Currencies::CBYRCurrency::GetShared ()
{
    if (_ppBYRCurrency == NULL) throw REFNEW NativeNullException ();
	return *_ppBYRCurrency;
}
QuantLib::BYRCurrency* Cephei::QL::Currencies::CBYRCurrency::GetPointer ()
{
    if (_ppBYRCurrency == NULL) throw REFNEW NativeNullException ();
	return &**_ppBYRCurrency;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BYRCurrency>& Cephei::QL::Currencies::CBYRCurrency::GetHandle ()
{
	if (_phBYRCurrency == NULL)
	{
		_phBYRCurrency = new Handle<QuantLib::BYRCurrency> (*_ppBYRCurrency);
	}
	return *_phBYRCurrency;
}
#endif
bool Cephei::QL::Currencies::CBYRCurrency::HasNative () 
{
	return (_ppBYRCurrency != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Currencies::IBYRCurrency^ Cephei::QL::Currencies::CBYRCurrency_Factory::Create ()
{
    return REFNEW CBYRCurrency ();
}
